<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="prototype-1.6.0.3.js"></script>
<script type="text/javascript">

function category_view()
{
	var url = 'http://localhost:9090/helloStruts2/category_view.action';
	var catename = $F('c_viewname');
	var pars = 'catename=' + catename;

	
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: category_view_response,
			onSuccess: function(transport) { 
			},
			onFailure: function(transport) {
			},
			onException: function(transport) {
			}
		});
	
}

function category_view_response(originalRequest)
{
	//put returned XML in the textarea
	$('category_div').innerHTML = originalRequest.responseText;	
}

function category_add()
{
	var url = 'http://localhost:9090/helloStruts2/category_add.action';
	
	var cmd = $F('category_name');
	
	
	var pars = 'catename=' + cmd;
	

	
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: category_addDel_response
		});
	
}

function category_addDel_response(originalRequest)
{
	//put returned XML in the textarea
	//$('category_div').innerHTML = originalRequest.responseText;
	alert(originalRequest.responseText);	
}

function category_del()
{
	var url = 'http://localhost:9090/helloStruts2/category_del.action';
	
	var cmd = $F('category_name');
	
	
	var pars = 'catename=' + cmd;
	

	
	new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onComplete: category_addDel_response
		});
	
}

function loadCategoryAdd()	//카테고리 불러오기(문제추가를 위한)
{
	//alert( "+++loadCategory" );
	
	var url = 'http://localhost:9090/helloStruts2/category_load_add.action';
	
	//var cmd = $F('category_name');
	
	
	var pars = 'catename=';
	

	
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars,
			onComplete: category_load_response
		});
	
}
function loadCategoryMod()	//카테고리 불러오기(문제수정을 위한)
{
//	alert( "+++loadCategory" );
	
	var url = 'http://localhost:9090/helloStruts2/category_load_mod.action';
	
	//var cmd = $F('category_name');
	
	
	var pars = 'catename=';
	

	
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars,
			onComplete: category_load_response2
		});
	
}

function loadCategoryAddExam()	//시험지에문제수정을위한 카테고리불러오기
{
	//alert( "+++loadCategory" );
	
	var url = 'http://localhost:9090/helloStruts2/category_load_addexam.action';
	
	//var cmd = $F('category_name');
	
	
	var pars = 'catename=';
	

	
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars,
			onComplete: category_load_response3
		});
	
}



function category_load_response(originalRequest)	//load결과 함수(문제추가에사용)
{	
	//alert(originalRequest.responseText);
	$('category_select').innerHTML = originalRequest.responseText;		
}

function category_load_response2(originalRequest)	//load결과 함수(문제수정에사용)
{	
	//alert(originalRequest.responseText);
	$('category_select2').innerHTML = originalRequest.responseText;		
}

function category_load_response3(originalRequest)	//load결과 함수(시험지문제추가에사용됨)
{	
	//alert(originalRequest.responseText);
	$('category_select3').innerHTML = originalRequest.responseText;		
}

function inputTest()	//문제 추가하는 함수('문제 입력' 버튼을 눌렀을때 동작)
{
	var url = 'http://localhost:9090/helloStruts2/inputTest.action';
	//alert(s1);
	var s2 = $F('examTitle');	//문제의 지문
	//alert(s2);
	var s3;	//답이 들어있는 번호(답을 구분하기 위해 존재)

	var example = "";	//보기저장변수
	
		
	var type = parseInt($F('question_type_select'));	//문제의 유형을 나타내는변수
											//1일때 주관식, 그외의 숫자는 보기의숫자

	if(type==1){
		//s3=$F('examCheck1');
		s3=1;
		example = $F('exam1');
			
	}else{
		for(a=1;a<=type;a++){
			if($F('examCheck'+a)!=null)
				s3=a;
			example+=$F('exam'+a);
			if(a!=type)
				example+="!@";			
		}		
	}
	
	//alert(s4);
	
	var c_idx = $F('select1');	//저장하려는 카테고리의 idx
	
	//var question=document.getElementById("examTitle").value;	//문제 지문
	var question=$F("examTitle");	//문제 지문	
	var answer = $F('exam'+s3);	//답
	var writer = '상콤';	//작성자
	//넘기려는 파라미터
	
	//var pars = 'example='+example+'&writer='+writer+'&answer='+answer+'&question='+question+'&c_category='+c_category;
		//alert(pars);
		//alert(question);
	var pars = {};
	pars["example"]=example;
	pars["writer"]=writer;
	pars["answer"]=answer;
	pars["question"]=question;
	pars["c_idx"]=c_idx;
	pars["type"]=type;

	alert("example : "+example+"\n"+"writer : "+writer+"\n"+"answer : "+answer
			+"\n"+"question : "+question+"\n"+"c_idx : "+c_idx+"\n"
			+"type : "+type);	
	new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars,
				onComplete: inputTest_response,
				onFailure : function(xxx) {
				}
			});		
}


function inputTest_response(originalRequest)
{	
	alert(originalRequest.responseText);
}

function load_Allquestion(){	// this is the function that call question to load
	
	var cmd=$F('select2');
//	alert(cmd);
	//alert(cmd);
	var url = 'http://localhost:9090/helloStruts2/questionAllload.action';	
	
	var pars = {};
	pars["idx"] = cmd;
	
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars,
			onComplete: load_Allquestion_response
			});	
}

function load_Allquestion_response(originalRequest)
{	
	//alert(originalRequest.responseText);
	$('question_result').innerHTML=originalRequest.responseText;
}

function load_question(){	//문제 하나 불러옴
	//alert("+++ 문제하나 불러오기 +++");
	var url = 'http://localhost:9090/helloStruts2/questionload.action';	
	var cmd = $F('select3');
	var pars = {};
	pars["idx"] = cmd;
	
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars,
			onComplete: load_question_response
		});
	
}

function load_question_response(originalRequest)
{	
	//alert(originalRequest.responseText);
	$('question_view').innerHTML=originalRequest.responseText;
}
function modify_question(){		//수정버튼 눌렀을때 동작하는 함수
	
	//alert($F('modchk1'));
	var url = 'http://localhost:9090/helloStruts2/questionmodify.action';
	var answer;	//수정한 답
	var example="";	//수정한 example(보기)
	var writer=$F('modwriter');
	var question=$F('modquestion');
	var idx = $F('modidx');
	var cmd = parseInt($F('modtype'));	//a가 반복하는수 즉,수정을 하기 위해선
		// 다시 주관식인지 객관식인지에 따라 다시,보기 정보가 들어있는 string값을 만들어야 하는데
		// 그를 위해선 현 문제의 타입을 가져와야 한다. loadQuestionResult.jsp에서 
		// type값을 가져온다.
	for(a=1;a<=cmd;a++){
		example+=$F('modtxt'+a);
		if(a!=cmd)
			example+="!@";
		if($F('modchk'+a)!=null){
			answer = $F('modtxt'+a);
		}
	}
	var pars={};
	pars["idx"]=idx;
	pars["writer"]=writer;
	pars["question"]=question;
	pars["answer"]=answer;
	pars["example"]=example;
	//pars["c_category"]=$F('select1');
	pars["c_category"]=$F('modcategory');
	pars["type"]=$F('modtype');

	
	new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars,
				onComplete: modify_question_response
			});	
//	alert("번호 : "+$F('modidx'));
//	alert("정답 : "+answer);
//	alert("보기 : "+example);
//	alert("작성자 : "+writer);
//	alert("지문 : "+question);
}

function modify_question_response(originalRequest)
{	
	alert(originalRequest.responseText);
	//$('question_view').innerHTML=originalRequest.responseText;
}

function delete_question(){
	var url = 'http://localhost:9090/helloStruts2/questiondelete.action';
	var idx = $F('modidx');
	var pars={};
	pars["idx"]=idx;
	new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars,
				onComplete: delete_question_response
			});		
}

function delete_question_response(originalRequest)
{	
	alert(originalRequest.responseText);
	//$('question_view').innerHTML=originalRequest.responseText;
	load_Allquestion();
}

function loadQuestionType(){	//문제의 유형을 선택하기 위한 함수
	var url = 'http://localhost:9090/helloStruts2/questiontype.action';
	var type = $F('question_type_select');
	var pars={};
	pars["type"]=type;
	new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars,
				onComplete: loadQuestionType_response
			});		
}

function loadQuestionType_response(originalRequest)
{	
	//alert(originalRequest.responseText);
	$('questiontype_selected').innerHTML=originalRequest.responseText;
	
}

function testlistload(){	//시험지 목록 불러오는 함수
	var url = 'http://localhost:9090/helloStruts2/testlistload.action';
	var type = $F('question_type_select');
	var pars={};
	pars["type"]=type;
	new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars,
				onComplete: testlistload_response
			});		
}

function testlistload_response(originalRequest)
{	
	//alert(originalRequest.responseText);
	$('test_list_p').innerHTML=originalRequest.responseText;
	
}

function testexamload(){		//해당 시험지의 문제불러오는 함수
	var url = 'http://localhost:9090/helloStruts2/testexamload.action';
	var type = $F('test_list_select');
	var pars={};
	pars["idx"]=type;
	alert(type);
	new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars,
				onComplete: testexamload_response
			});
}

function testexamload_response(originalRequest)
{	
	//alert(originalRequest.responseText);
	$('test_exam_list_p').innerHTML=originalRequest.responseText;
	
}

function examdel(){		//해당 시험지를 삭제하는 함수
	var url = 'http://localhost:9090/helloStruts2/examdel.action';
	var type = $F('test_list_select');	//선택된 시험의 idx값
	var pars={};
	pars["idx"]=type;
	new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars,
				onComplete: examdel_response
			});
}

function examdel_response(originalRequest)
{	
	alert(originalRequest.responseText);
	//$('test_exam_list_p').innerHTML=originalRequest.responseText;
	testlistload();
	
}

function testexamdel(){
	//alert($F('q_idx'));
	var url = 'http://localhost:9090/helloStruts2/testexamdel.action';
	var cmd = $F('test_exam_list_select');	//선택된 시험의 idx값
	var pars={};
	
	pars["q_idx"]=$F('q_idx');
	pars["selected_idx"]=cmd;
	pars["idx"]=$F('exam_idx');
	new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars,
				onComplete: testexamdel_response
			});	
}

function testexamdel_response(originalRequest)
{	
	//alert(originalRequest.responseText);
	$('test_exam_list_p').innerHTML=originalRequest.responseText;
	testexamload();
}

function testexamquestionload(){	// this is the function that call question to load
	
	var cmd=$F('select4');
//	alert(cmd);
	//alert(cmd);
	var url = 'http://localhost:9090/helloStruts2/testexamquestionload.action';	
	
	var pars = {};
	pars["idx"] = cmd;
	
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars,
			onComplete: testexamquestionload_response
		});	
}

function testexamquestionload_response(originalRequest)
{	
	//alert(originalRequest.responseText);
	$('test_category_exam_list_p').innerHTML=originalRequest.responseText;
}

function testexamadd(){	// this is the function that call question to load
//	alert($F('test_list_select'));
//	alert($F('q_idx'));
//	alert($F('test_category_exam_list_select'));
	var cmd=$F('select4');
//	alert(cmd);
	//alert(cmd);
	var url = 'http://localhost:9090/helloStruts2/testexamadd.action';	
	
	var pars = {};
	pars["selected_idx"] = $F('test_category_exam_list_select');
	pars["q_idx"] = $F('q_idx');
	pars["idx"] =  $F('test_list_select');
		
	new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars,
			onComplete: testexamadd_response
		});	
}

function testexamadd_response(originalRequest)
{	
	alert(originalRequest.responseText);
	//$('test_category_exam_list_p').innerHTML=originalRequest.responseText;
	testexamload();
}


</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
-----카테고리-----
<input type="text" size="10" id="c_viewname"><input type="button" value="조회" onclick="category_view()"><br>
<div id="category_div"></div>
<p id="category_p">
이름 : <input type="text" size="10" id="category_name"><input type="button" value="추가" onclick="category_add()"><input type="button" value="삭제">
</p>
-----문제관리 -----<br>
-추가-
<p id="category_select">
<input type="button" value="카테고리불러오기" onclick="loadCategoryAdd()">
<select id="select1">
<option value="">선택하세요
</option>
</select>
</p>
지문입력 :&nbsp;<input type="text" size="50" id="examTitle"><br>

선택:<br>
<select id="question_type_select" onchange="loadQuestionType()">
<option value="">선택하세요</option>
<option value="1">주관식</option>
<option value="4">4지선다형</option>
<option value="5">5지선다형</option>
<option value="6">6지선다형</option>
</select>
<p id="questiontype_selected"/>	




<br>
-수정-<br>
<p id="category_select2">
<input type="button" value="카테고리불러오기" onclick="loadCategoryMod()">
<select id="select2">
<option value="">선택하세요
</option>
</select>
</p>
<table>
	<tr>
    <td>
		<p id="question_result">
	    <select id="select3" size="10" >
	    <option>---문제 리스트---</option>
	    </select>
		</p>
	</td>
	<td>
<!-- 선택된 문제의 정보가 보여지고 수정할수있는 폼 -->
		<p id="question_view">
		</p>
		<input type="button" value="수정" onclick="modify_question()">
		<input type="button" value="삭제" onclick="delete_question()">		
	</td>
	</tr>
</table>

-----시험지관리-----<br>

---조회---<br>
<input type="text"><input type="button" value="조회" onclick="testlistload()">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<p id="category_select3">
<input type="button" value="카테고리불러오기" onclick="loadCategoryAddExam()">
<select id="select4">
<option value="">선택하세요
</option>
</select>
</p>
<br>
<table>
	<tr>
	    <td>
	    	<!-- 시험지목록 -->
			<p id="test_list_p">
		    <select id="test_list_select" size="10" >
		    <option>---시험지 리스트---</option>
		    </select>
			</p>
		</td>
		<td>
			<!-- 해당문제지의 문제리스트 보여줌 -->
			<p id="test_exam_list_p">
		    <select id="test_exam_list_select" size="10" >
		    <option>---시험지문제리스트---</option>
		    </select>			
		    </p>
		</td>
		<td>
			<!-- 해당카테고리의문제목록 -->
			<p id="test_category_exam_list_p">
		    <select id="test_category_exam_list_select" size="10" >
		    <option>---문제리스트---</option>
		    </select>
		    </p>			
		</td>
	</tr>
	<tr>
		<td>
			<input type="button" value="삭제" onclick="examdel()">
		</td>
		<td>
			<input type="button" value="삭제" onclick="testexamdel()">	
		</td>
		<td>
			<input type="button" value="추가" onclick="testexamadd()">
		</td>
				
	</tr>
</table>

</body>
</html>